/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package adee.qrlogin;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.lang.String;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author adee
 */
public class Accounts extends ListActivity {

  
    private List<Account> accounts;
    private Context context;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.context = this;
        AccountsDB db = new AccountsDB(this);
        accounts =  db.getAccounts();
        setListAdapter(new ArrayAdapter(this, R.layout.accounts, accounts.toArray()));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            private Account account;
            public boolean onItemLongClick(AdapterView<?> av, View view, int i, long l) {
                     account = accounts.get(i);
                    new AlertDialog.Builder(context)
                       .setIcon(R.drawable.kerdojel)
                       .setTitle(R.string.logout)
                       .setMessage(R.string.really_logout)
                       .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                           @Override
                            public void onClick(DialogInterface dialog, int which) {
                               account.logout();
                             }

                       })
                      .setNegativeButton(R.string.no, null)
                      .show();
                   return true;
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> av, View view, int i, long l) {
                Intent   intent = new Intent(context, AccountFormActivity.class);
                int id= accounts.get(i).getId();
                intent.putExtra("id",String.valueOf(id));
	        startActivity(intent);
            }
        });
    }

}
